home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / graphics / stadconv.lzh / STADCONV / TURBO_C / TOSTAD.S < prev   
Text File  |  1992-07-11  |  11KB  |  202 lines

  1. *****************************************************************************
  2. *                                                                           *
  3. * Ass-Subroutine to convert a 32k-picture (screenformat) to a packed        *
  4. * STAD-format.                                                              *
  5. *                                                                           *
  6. * params:   (sp) LONG  return-adress                                        *
  7. *          4(sp) LONG  source-adress (32k buffer)                           *
  8. *          8(sp) LONG  dest-adress of STAD-pic (33k buffer)                 *
  9. *                                                                           *
  10. * returns:   do  WORD  length of packed STAD-picture                        *
  11. *                                                                           *
  12. * important: the two adresses are not removed from the stack (like trap)    *
  13. *                                                                           *
  14. * History:                                                                  *
  15. * Version 1.0   01.06.1991  Wolfgang Ley                                    *
  16. *                           - first version                                 *
  17. * Version 1.1   03.06.1991  Wolfgang Ley                                    *
  18. *                           - saving registers                              *
  19. *                           - improved statistics                           *
  20. *                           - no extra statistics-buffer anymore            *
  21. * Version 1.2   05.06.1991  Wolfgang Ley                                    *
  22. *                           - extra counter in v_pack removed               *
  23. * Version 1.3   25.06.1992  Jens Dittmer                                    *
  24. *                           - bug in h_pack removed (header)                *
  25. * Version 1.4   29.06.1992  Jens Dittmer                                    *
  26. *                           - algorithm improved -> much smaller code       *
  27. * Version 1.5   01.07.1992  Jens Dittmer                                    *
  28. *                           - improved statistics                           *
  29. * Version 1.5b  11.07.1992  Wolfgang Ley                                    *
  30. *                           - extra rts removed                             *
  31. *                                                                           *
  32. *****************************************************************************
  33.  
  34.                 GLOBL tostad
  35.  
  36.                 TEXT
  37.  
  38. tostad:         movem.l D1-A1,-(SP)     ;save registers
  39.  
  40.                 movea.l 40(SP),A0       ;adress of 32k-picture
  41.                 movea.l 44(SP),A1       ;adress of packes STAD-picture
  42.  
  43. ;----------------------------------------------------------------------------
  44. ; make some staticts (get most frequently used and the two less used bytes)
  45. ; --> choose PACK-/ID-/SPECIAL-byte
  46. ;----------------------------------------------------------------------------
  47.  
  48.                 move.w  #511,D0         ;index-register
  49. stat_1:         clr.b   0(A1,D0.w)      ;clear byte-counters
  50.                 dbra    D0,stat_1
  51.  
  52.                 move.w  #31999,D0       ;pointer within the picture
  53. stat_2:         move.b  0(A0,D0.w),D1   ;get byte
  54.                 add.w   D1,D1           ;double it
  55.                 andi.w  #$01FF,D1       ;clear the rest
  56.                 addq.w  #1,0(A1,D1.w)   ;and count byte
  57.                 dbra    D0,stat_2       ;and so on...
  58.  
  59.                 move.w  #510,D0         ;most frequently used byte till now
  60.                 move.w  0(A1,D0.w),D1   ;get quantity
  61.                 move.w  #508,D2         ;counter to the rest
  62. stat_3:         cmp.w   0(A1,D2.w),D1   ;compare
  63.                 bhs.b   stat_4
  64.                 move.w  D2,D0           ;remember position
  65.                 move.w  0(A1,D2.w),D1   ;and remember the quantity
  66. stat_4:         subq.w  #2,D2           ;next entry
  67.                 bpl.b   stat_3          ;and again...
  68.                 lsr.w   #1,D0           ;that's the PACK-byte
  69.                 move.b  D0,D3           ;PACK-byte to D3
  70.  
  71.                 move.w  #510,D0         ;less used byte till now
  72.                 move.w  0(A1,D0.w),D1   ;get quantity
  73.                 move.w  #508,D2         ;counter to the rest
  74. stat_5:         cmp.w   0(A1,D2.w),D1   ;compare
  75.                 blo.b   stat_6
  76.                 move.w  D2,D0           ;remember position
  77.                 move.w  0(A1,D2.w),D1   ;...and the quantity
  78.                 beq.b   stat_7          ;number=0 => exit loop
  79. stat_6:         subq.w  #2,D2           ;next counter
  80.                 bpl.b   stat_5          ;and continue...
  81. stat_7:         move.w  #32001,0(A1,D0.w) ;"clear" entry
  82.                 lsr.w   #1,D0           ;that's the ID-bByte
  83.                 move.b  D0,D4           ;ID-byte to D4
  84.  
  85.                 move.w  #510,D0         ;like above...
  86.                 move.w  0(A1,D0.w),D1
  87.                 move.w  #508,D2         
  88. stat_8:         cmp.w   0(A1,D2.w),D1
  89.                 blo.b   stat_9
  90.                 move.w  D2,D0
  91.                 move.w  0(A1,D2.w),D1
  92.                 beq.b   stat_a
  93. stat_9:         subq.w  #2,D2
  94.                 bpl.b   stat_8
  95. stat_a:         lsr.w   #1,D0           ;that's the SPEC-byte
  96.                 move.b  D0,D5           ;SPEC-Byte to D5
  97.  
  98. ;----------------------------------------------------------------------------
  99. ; pack horizontal and vertical, use smaller one as STAD-picture
  100. ; (validate if the compressed picture is really smaller - if not, use the
  101. ;  uncompressed picture)
  102. ;----------------------------------------------------------------------------
  103.  
  104.                 clr.w   D0              ;clear offset
  105.                 move.b  #'p',0(A1,D0.w) ;write header #'pM85'
  106.                 move.b  #'M',1(A1,D0.w) ;(bytes, because address may be odd)
  107.                 move.b  #'8',2(A1,D0.w) ;'5' or '6' is appended at packing
  108.                 move.b  D4,4(A1,D0.w)   ;write ID-Byte
  109.                 move.b  D3,5(A1,D0.w)   ;write PACK-Byte
  110.                 move.b  D5,6(A1,D0.w)   ;write SPEC-Byte
  111.  
  112.                 bsr.b   h_pack          ;pack horizontal
  113.                 move.w  D0,D7           ;remember length (horiz.)
  114.                 bsr.b   v_pack          ;pack vertical
  115.                 cmp.w   D7,D0           ;vertical smaller?
  116.                 bls.b   check           ;yes -> everything ok
  117.                 bsr.b   h_pack          ;no  -> pack again horizontal
  118. check:          cmpi.w  #32000,D0       ;is the packed picture really
  119.                 blo.b   exit            ;smaller?
  120.                 move.w  #31999,D0       ;if not, copy original 32k-pic
  121. copy:           move.b  (A0)+,(A1)+    
  122.                 dbra    D0,copy
  123.                 move.w  #32000,D0       ;length is 32000 bytes
  124. exit:           movem.l (SP)+,D1-A1     ;restore registers
  125.                 rts                     ;that's it
  126.  
  127. ;----------------------------------------------------------------------------
  128. ; horizontal compress
  129. ;----------------------------------------------------------------------------
  130.  
  131. h_pack:         clr.w   D6              ;source-index
  132.                 moveq   #7,D0           ;length of header
  133.                 move.b  #'5',3(A1,D6.w)
  134. h_pack_1:       clr.w   D1              ;sequence-length 0
  135.                 move.b  0(A0,D6.w),D2   ;read byte
  136. h_pack_2:       addq.w  #1,D6           ;next column
  137.                 cmpi.w  #32000,D6       ;ready?
  138.                 beq.b   emit            ;write code and terminate
  139.                 cmpi.w  #255,D1         ;max. seq.-length?
  140.                 beq.b   h_pack_3        ;write code and start new sequence
  141.                 cmp.b   0(A0,D6.w),D2   ;next source-byte <> seq.byte?
  142.                 bne.b   h_pack_3        ;write code and start new sequence
  143.                 addq.w  #1,D1           ;increment length
  144.                 bra.b   h_pack_2        ;and read next...
  145. h_pack_3:       bsr.b   emit            ;store sequence
  146.                 bra.b   h_pack_1        ;and start new sequence
  147.  
  148. ;-----------------------